using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Gadgets.Setups
{
    public partial class eRoles : WeifenLuo.WinFormsUI.Docking.DockContent  
    {
        public eRoles()
        {
            InitializeComponent();
        }

        private void eRoles_Load(object sender, EventArgs e)
        {
            try
            {
                ABSCommonLibrary.CreateGridView(ref gridControl1);
                DataSet menus = ABSCommonLibrary.DoInquiry(
                    CommonLibrary.CommonLibrary.BOSC019002, new object[1] { "M" });

                if (menus != null)
                {
                    foreach (DataRow row in menus.Tables[0].Rows)
                    {
                        if (Convert.ToInt32(row["PARENT_ID"]) == 0)
                        {
                            TreeNode root = new TreeNode();
                            root.Text = row["MENU_TEXT"].ToString();
                            root.Tag = row["MENU_ID"];
                            treeViewMenu.Nodes.Add(root);
                            AddTreeNode(ref root, menus);
                        }
                    }
                }
                //menu_count = menus.Tables[0].Rows.Count;
                treeViewMenu.ExpandAll();
                //Build menu report
                menus = ABSCommonLibrary.DoInquiry(
                    CommonLibrary.CommonLibrary.BOSC019002, new object[1] { "R" });

                if (menus != null)
                {
                    foreach (DataRow row in menus.Tables[0].Rows)
                    {
                        if (Convert.ToInt32(row["PARENT_ID"]) == 0)
                        {
                            TreeNode root = new TreeNode();
                            root.Text = row["MENU_TEXT"].ToString();
                            root.Tag = row["MENU_ID"];
                            treeViewReport.Nodes.Add(root);
                            AddTreeNode(ref root, menus);
                        }
                    }
                }
                //menu_count = menus.Tables[0].Rows.Count;
                treeViewReport.ExpandAll();

            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }
        private void AddTreeNode(ref TreeNode parent, DataSet menus)
        {
            foreach (DataRow row in menus.Tables[0].Rows)
            {
                if (Convert.ToInt32(row["PARENT_ID"]) == Convert.ToInt32(parent.Tag))
                {
                    TreeNode child = new TreeNode();
                    child.Text = row["MENU_TEXT"].ToString();
                    child.Tag = row["MENU_ID"];
                    parent.Nodes.Add(child);
                    AddTreeNode(ref child, menus);
                }
            }
        }
        private void uncheck_allnodes(TreeNode nodes)
        {
            nodes.Checked = false;
            foreach (TreeNode node in nodes.Nodes)
            {
                uncheck_allnodes(node);
            }
        }
        private void ShowUserMenus(TreeNode parent, DataSet menus)
        {
            foreach (DataRow row in menus.Tables[0].Rows)
            {
                if (Convert.ToInt32(parent.Tag) == Convert.ToInt32(row["MENU_ID"]))
                {
                    parent.Checked = true;
                    break;
                }
            }
            foreach (TreeNode child in parent.Nodes)
            {
                ShowUserMenus(child, menus);
            }
        }
        private void UpdateTreeNode(TreeNode parent, int user_id)
        {
            //CenturyFox.Service data_access = new CenturyFox.Service();
            string status = "S";
            if (parent.Checked)
                status = "A";
            object[] p_param = new object[3] { user_id, parent.Tag, status };
            ABSCommonLibrary.DoExecute(
                CommonLibrary.CommonLibrary.BOSC016005, ref p_param);
            foreach (TreeNode child in parent.Nodes)
            {
                UpdateTreeNode(child, user_id);
            }
        }
        private void eRoles_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F11)
            {
                if (gridControl1.DataRows.Count == 0)
                    return;
                int user_id = Convert.ToInt32(gridControl1.CurrentCell.ParentRow.Cells["USER_ID"].Value);
                UpdateTreeNode(treeViewMenu.Nodes[0], user_id);
                UpdateTreeNode(treeViewReport.Nodes[0], user_id);
            }
            else if (e.KeyCode == Keys.F9)
            {
                string user_name = string.Empty;
                if (valueRow1.Cells["NAME"].Value != null)
                    user_name = valueRow1.Cells["NAME"].Value.ToString();
                //string role = IceAge.Administration.CommonLibrary.DoExecute("0101", new object[2] {"hoanvk", "kijuto" }).ToString();
                DataSet users = ABSCommonLibrary.DoInquiry(
                    CommonLibrary.CommonLibrary.BOSC019001,
                    new object[2] { user_name, 0 });
                gridControl1.SetDataBinding(users, users.Tables[0].TableName);
            }
        }

        private void gridControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter)
                return;
            if (gridControl1.DataRows.Count == 0)
                return;
            //CenturyFox.Service data_access = new CenturyFox.Service();
            string user_name = string.Empty;
            user_name = gridControl1.CurrentCell.ParentRow.Cells["NAME"].GetDisplayText();

            DataSet menus = ABSCommonLibrary.DoInquiry(
                CommonLibrary.CommonLibrary.BOSC020001, new object[2] { user_name, "M" });

            //reset treeview
            foreach (TreeNode node in treeViewMenu.Nodes)
            {
                uncheck_allnodes(node);
            }
            foreach (TreeNode node in treeViewReport.Nodes)
            {
                uncheck_allnodes(node);
            }
            //end reset

            if (menus != null)
            {
                ShowUserMenus(treeViewMenu.Nodes[0], menus);
            }

            menus = ABSCommonLibrary.DoInquiry(
                CommonLibrary.CommonLibrary.BOSC020001, new object[2] { user_name, "R" });
            if (menus != null)
            {
                ShowUserMenus(treeViewReport.Nodes[0], menus);
            }
        }

    }
}